﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using System.Net.Mail;
using LeadVinaglue.Models;
using LeadVinaglue.Models.ViewPages;

namespace LeadVinaglue.Repository
{
    public partial class ContentRepository : BaseManagementRepository
    {
        LeadVinaglueEntities entities = new LeadVinaglueEntities();

        #region #Model
        public ContentManagementModel GetModel(string pageType, string roleName, int id)
        {
            ContentManagementModel model = new ContentManagementModel();

            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListContent = GetAll();

            if (id > 0)
            {
                model.ContentInfo = GetSingle(id);
            }

            return model;
        }
        public ContentManagementModel GetDeleteModel(string pageType, string roleName, int id)
        {
            ContentManagementModel model = new ContentManagementModel();

            if (id > 0)
            {
                Delete(id);
            }
            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
            model.ListContent = GetAll();


            return model;
        }

        public ContentManagementModel GetModel(string pageType, string roleName)
        {
            ContentManagementModel model = new ContentManagementModel();
            model.HeaderView = GetHeader();
            model.MenuItem = GetMenuItems(roleName, pageType);
           // model.ListContent = GetAll();

            return model;
        }

        public List<ContentModel> GetAll()
        {
            List<ContentModel> list = new List<ContentModel>();

            var items = (from c in entities.ApplicationContents
                         orderby c.DateCreated descending
                         select c).ToList();

            foreach (var item in items)
            {
                ContentModel model = new ContentModel();

                model.ID = item.ID;
                model.PageName = item.PageName;
                model.Title = item.Title;
                model.DateCreated = Convert.ToDateTime(item.DateCreated).ToString("dd-MM-yyyy");

                list.Add(model);
            }

            return list;
        }
        #endregion

        public ApplicationContent GetSingle(int id)
        {
            return (from c in entities.ApplicationContents where c.ID == id select c).FirstOrDefault();
        }

        public ApplicationContent GetSingle(string pageName)
        {
            return (from _object in entities.ApplicationContents where _object.PageName == pageName select _object).FirstOrDefault();
        }

        public void Create([Bind(Exclude = "ID, DateCreated")] ApplicationContent _object)
        {
            _object.DateCreated = System.DateTime.Now;
             entities.AddToApplicationContents(_object);
            entities.SaveChanges();
        }

        public void Delete(int id)
        {
            var _object = (from _list in entities.ApplicationContents where _list.ID == id select _list).First();
            entities.DeleteObject(_object);
            entities.SaveChanges();
        }

        public void Save([Bind(Exclude = "ID")] ApplicationContent _object)
        {
            _object.EntityKey = (from ApplicationContent ac in entities.ApplicationContents where ac.ID == _object.ID select ac).FirstOrDefault().EntityKey;
            _object.DateCreated = System.DateTime.Now;
            entities.ApplyPropertyChanges(_object.EntityKey.EntitySetName, _object);
            entities.SaveChanges();
        }
    }
}
